layui.use(["jquery","laydate","form"],function () {
    var
        $ = layui.jquery,
        laydate = layui.laydate,
        form = layui.form;
    laydate.render({
        elem:"#startDate",
        type:'datetime'
    });
    laydate.render({
        elem:"#endDate",
        type:'datetime'
    });
    //刷新
    $("#Refresh").click(function () {
        location.reload();
    });
    //返回
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    $.ajax({
        url:"/JsUserCoupon/getAllJsUserCoupon",
        data:{},
        type:'post',
        dataType:'json',
        success:function (data) {
            $("#submitBtn").click(function () {
                var createDate = "";
                getTime();
                //现在时间
                function getTime(){
                    var date=new Date();
                    var year=date.getFullYear();
                    var month=date.getMonth()+1;
                    var day=date.getDate();
                    var hour=date.getHours();
                    var minute=date.getMinutes();
                    var second=date.getSeconds();
                    var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
                    createDate = dateStr;
                }
                function zeroFilling(data){
                    if(data<10){
                        data="0"+data;
                    }
                    return data;
                }
                var layForm = form.val('layForm');
                var couponCode = layForm.couponCode;
                var arr = [];
                for (var i = 0; i < data.data.length; i++) {
                    arr.push(data.data[i].couponCode);
                }
                var num = 0;
                for (var i = 0; i <= arr.length ; i++) {
                    if (arr[i] == couponCode) {
                        layer.msg("已存在商品编号");
                        return ;
                    }
                    if (arr[i] != couponCode&&arr.length!=0) {
                        num++;
                    }
                    if (num == arr.length) {
                        var userId = JSON.parse(localStorage.getItem('user')).data.userId;
                        var param = {
                            'couponName':layForm.couponName,
                            'couponPrice':layForm.couponPrice,
                            'startDate':layForm.startDate,
                            'endDate':layForm.endDate,
                            'online':layForm.online,
                            'issueQuantity':layForm.issueQuantity,
                            'grant':layForm.grant,
                            'type':layForm.type,
                            'dataDictValue':layForm.dataDictValue,
                            'couponCode':couponCode,
                            'createId':userId,
                            'createDate':createDate
                        };
                        console.log(param);
                        $.ajax({
                            url:"/JsUserCoupon/addJsUserCoupon",
                            data:param,
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                                $.ajax({
                                    url:"/JsUserCoupon/selectByIdJsUserCoupon",
                                    data:{'couponCode':couponCode},
                                    type:'post',
                                    dataType:"json",
                                    success:function (data) {
                                        var couponId = data.data.couponId;
                                        var dataDictValue = layForm.dataDictValue;
                                        var dictData = {
                                            'dataDictLabel':couponId,
                                            'dataDictValue':dataDictValue
                                        };
                                        $.ajax({
                                            url:"/JsUserCoupon/addDictData",
                                            data:dictData,
                                            type:'post',
                                            dataType:'json',
                                            success:function (data) {
                                                window.location.href="/coupon/list/coupon-list.html";
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                }
            });
        }
    });

    upper = function upper() {
        window.location.href="/coupon/list/coupon-list.html";
    }
});